package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.DictTool;
import com.csjy.migration.hnnd.MD5Utils;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.FormContentUrl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@BusinessDataConverter(
    auto = false,

    updateSql = "UPDATE ACT_HI_VARINST\n" +
    "SET PROC_INST_ID_=?, EXECUTION_ID_=?, TASK_ID_=?, NAME_=?, VAR_TYPE_=?, REV_=?, BYTEARRAY_ID_=?, DOUBLE_=?, LONG_=?, TEXT_=?, TEXT2_=?, CREATE_TIME_=?, LAST_UPDATED_TIME_=?\n" +
    "WHERE ID_ = ?",

    insertSql =  "INSERT INTO ACT_HI_VARINST\n" +
    "(ID_, PROC_INST_ID_, EXECUTION_ID_, TASK_ID_, NAME_, VAR_TYPE_, REV_, BYTEARRAY_ID_, DOUBLE_, LONG_, TEXT_, TEXT2_, CREATE_TIME_, LAST_UPDATED_TIME_)\n"+
    "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",

    findMigratedIdListSql = "SELECT ID_ FROM ACT_HI_VARINST WHERE PROC_INST_ID_ = ?"
)
public class ActHiVarinst extends AbstractWorkflowBusiness {

  public ActHiVarinst(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId, String procInstId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId, procInstId);
  }

  public void migrate() {

  }

  public void customMigrate(Map<String,Object> map) throws SQLException {

    for (Map.Entry<String, Object> entry : map.entrySet()) {
      int needMigratedResult = this.needMigrated(MD5Utils.stringToMD5(this.procInstId+entry.getKey()));
      if(needMigratedResult == 0) {
        continue;
      }
      if(needMigratedResult == 1) {
        this.targetDbUtil.updateByParams(updateSql, this.buildValues(entry.getKey(), entry.getValue()), true);
      } else {
        this.targetDbUtil.updateByParams(insertSql, this.buildValues(entry.getKey(), entry.getValue()), false);
      }
    }
  }

  public List buildValues(String varName,Object varValue) throws SQLException {
    List values = new ArrayList();
    values.add(MD5Utils.stringToMD5(this.procInstId+varName));
    values.add(this.procInstId + "-JW");
    values.add(this.procInstId + "-JW");
    values.add(null);
    values.add(varName);
    if(varValue instanceof Integer) {
      values.add("integer");
    } else {
      values.add("string");
    }
    values.add(1);
    values.add(null);
    // DOUBLE_
    values.add(null);
    // LONG_
    if(varValue instanceof Integer) {
      values.add(varValue);
    } else {
      values.add(null);
    }
    // TEXT_
    values.add(varValue);
    // TEXT2_
    values.add(null);
    values.add(new java.sql.Date(System.currentTimeMillis()));
    values.add(new java.sql.Date(System.currentTimeMillis()));
    return values;
  }


}
